package com.felix.thread.semaphore;

import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * MySemaphore
 *
 * @author xuejinhua
 * @date 2019/1/14 16:56
 */
public class MySemaphore {

    public static void main(String[] args) throws IOException, InterruptedException {
        final File stream = new File("c:\\temp\\stonefeng\\stream.txt");
        final OutputStream os = new FileOutputStream(stream);
        final OutputStreamWriter writer = new OutputStreamWriter(os);
        final Semaphore semaphore = new Semaphore(10);
        ExecutorService exec = Executors.newCachedThreadPool();

        final long start = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            final int num = i;
            Runnable task = () -> {
                try {
                    semaphore.acquire();
                    writer.write(String.valueOf(num) + "\n");
                    semaphore.release();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            };
            exec.submit(task);
        }
        exec.shutdown();
        exec.awaitTermination(1, TimeUnit.HOURS);
        writer.write("---END---\n");
        writer.close();
        System.out.println("ËùÓÐµÄ×ÓÏß³Ì¶¼½áÊøÁË£¡");
        final long end = System.currentTimeMillis();
        System.out.println((end - start) / 1000);
    }
}
